﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Excel2doc
{
    public partial class setting : Form
    {
        string path = Application.StartupPath + "\\config.xml";
        public setting()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.Copy(path,path+DateTime.Now.ToString("HHmmss")+".bak");
            DataTable data = new DataTable("map");
            data.Columns.Add("书签", typeof(string));
            data.Columns.Add("列名", typeof(string));

            DataRow row = data.NewRow();
            row["书签"] = "BOOKMARK0";
            row["列名"] = "列名测试";
            data.Rows.Add(row);

            FileStream fs = new FileStream(path, FileMode.Create);
            //DataSet ds = dataGridView1.DataSource as DataSet;                        
            DataSet ds = new DataSet();
            ds.Tables.Add(data);
            ds.WriteXml(fs);
            fs.Close();
            DataSet ds1 = new DataSet();
            ds1.ReadXml(path);
            dataGridView1.DataSource = ds1.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable data = new DataTable("map");
            data.Columns.Add("书签", typeof(string));
            data.Columns.Add("列名", typeof(string));
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.IsNewRow)
                    continue;
                DataRow row = data.NewRow();
                row["书签"] = r.Cells["书签"].Value;
                row["列名"] = r.Cells["列名"].Value;
                data.Rows.Add(row);
            }

            FileStream fs = new FileStream(path, FileMode.Create);                                   
            DataSet ds = new DataSet();
            ds.Tables.Add(data);
            ds.WriteXml(fs);
            fs.Close();
            DataSet ds1 = new DataSet();
            ds1.ReadXml(path);
            dataGridView1.DataSource = ds1.Tables[0];
        }

        private void setting_Load(object sender, EventArgs e)
        {
            DataSet ds1 = new DataSet();
            ds1.ReadXml(path);
            dataGridView1.DataSource = ds1.Tables[0];
        }
    }
}
